package com.wuchao.util;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import javax.annotation.Resource;
import java.io.IOException;

/**
 * @Author wuchao66
 * @Date 2023/9/15 16:27
 * @PackageName:com.wuchao.util
 * @ClassName: Utils
 * @Description: TODO
 * @Param
 * @Version 1.0
 */
public class Utils {//串
    //谁吊我，就把全部组装好，给我一个sqlsession
    static {//静态块
        init();
    }

    private static SqlSessionFactory sf;//工厂
    //初始化工厂
    private static  void init(){
        //工人建厂 有图纸
        try {
            sf= new SqlSessionFactoryBuilder().build(
                    Resources.getResourceAsStream("mybatis-config.xml")
            );
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    //生成产品
    public  static SqlSession getSqlSession(){
        return sf.openSession(true);//出产品 select 事务
    }
    //关闭产品
    public  static void closeSqlSession(SqlSession sqlSession){
        if(sqlSession!=null){
            sqlSession.close();//不空你来关
        }
    }
}
